CREATE TABLESPACE
#CREATE TABLESPACE
CREATE TABLESPACE — Define a new tablespace
Synopsis
CREATE TABLESPACE tablespace_name
[ OWNER { new_owner | CURRENT_USER | SESSION_USER } ]
LOCATION 'directory'
[ WITH ( tablespace_option = value [, ... ] ) ]
Description
CREATE TABLESPACE registers a new cluster-wide tablespace. The tablespace name must be distinct from the name of any existing tablespace in the database cluster.
A tablespace allows superusers to define an alternative location on the file system where data files containing database objects (such as tables and indexes) can be stored.
A user with appropriate privileges can pass tablespace_name to CREATE DATABASE, CREATE TABLE, CREATE INDEX, or ADD CONSTRAINT to have the data files of these objects stored within the specified tablespace.
Parameters
tablespace_name
The name of a tablespace to be created. The name cannot begin with pg_, as such names are reserved for system tablespaces.
user_name
The name of the user who will own the tablespace. If omitted, defaults to the user executing the command. Only superusers can create tablespaces, but they can assign ownership of tablespaces to non-superusers.
directory
The directory to be used for the tablespace. The directory must exist (CREATE TABLESPACE will not create it), should be empty, and must be owned by the system user. The directory must be specified with an absolute path.
tablespace_option
A tablespace parameter to be set or reset. Currently, the only available parameters are seq_page_cost, random_page_cost, effective_io_concurrency, and maintenance_io_concurrency. Setting these values for a particular tablespace will override the planner's usual cost estimates and the executor's prefetch behavior for table pages read from that tablespace, as determined by the configuration parameters of the same names (see seq_page_cost, random_page_cost, effective_io_concurrency, maintenance_io_concurrency). These parameters can be useful if a tablespace is located on a disk that is slower or faster than other I/O subsystems.
Notes
Tablespaces are only supported on systems that support symbolic links.
CREATE TABLESPACE cannot be executed inside a transaction block.
Examples
# To create a tablespace dbspace at file system location /data/dbs, first use operating system tools to create the directory and set proper ownership:
mkdir /data/dbs
chown halo:halo /data/dbs
# Then use the tablespace creation command:
CREATE TABLESPACE dbspace LOCATION '/data/dbs';
# To create a tablespace owned by a different database user, use a command like this:
CREATE TABLESPACE indexspace OWNER genevieve LOCATION '/data/indexes';
See Also
CREATE DATABASE, CREATE TABLE, CREATE INDEX, DROP TABLESPACE, ALTER TABLESPACE